
package vip.xiaonuo.biz.modular.lancerUserGoods.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.biz.modular.lancerGoodsBox.service.BizGoodsBoxService;
import vip.xiaonuo.biz.modular.pojo.domain.BizGoodsBox;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.biz.modular.lancerUserGoods.entity.BizUserGoods;
import vip.xiaonuo.biz.modular.lancerUserGoods.mapper.BizUserGoodsMapper;
import vip.xiaonuo.biz.modular.lancerUserGoods.param.BizUserGoodsAddParam;
import vip.xiaonuo.biz.modular.lancerUserGoods.param.BizUserGoodsEditParam;
import vip.xiaonuo.biz.modular.lancerUserGoods.param.BizUserGoodsIdParam;
import vip.xiaonuo.biz.modular.lancerUserGoods.param.BizUserGoodsPageParam;
import vip.xiaonuo.biz.modular.lancerUserGoods.service.BizUserGoodsService;
import vip.xiaonuo.common.pojo.DbEnum;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单Service接口实现类
 *
 * @author Lancer
 * @date 2024/03/03 17:11
 **/
@Service
public class BizUserGoodsServiceImpl extends ServiceImpl<BizUserGoodsMapper, BizUserGoods> implements BizUserGoodsService {

    @Override
    public Page<BizUserGoods> page(BizUserGoodsPageParam bizUserGoodsPageParam) {
        QueryWrapper<BizUserGoods> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(bizUserGoodsPageParam.getUserName())) {
            queryWrapper.lambda().like(BizUserGoods::getUserName, bizUserGoodsPageParam.getUserName());
        }
        if (ObjectUtil.isNotEmpty(bizUserGoodsPageParam.getBrandName())) {
            queryWrapper.lambda().like(BizUserGoods::getBrandName, bizUserGoodsPageParam.getBrandName());
        }
        if (ObjectUtil.isNotEmpty(bizUserGoodsPageParam.getGoodsName())) {
            queryWrapper.lambda().like(BizUserGoods::getGoodsName, bizUserGoodsPageParam.getGoodsName());
        }
        if (ObjectUtil.isNotEmpty(bizUserGoodsPageParam.getPayMoney())) {
            queryWrapper.lambda().ge(BizUserGoods::getPayMoney, bizUserGoodsPageParam.getPayMoney());
        }
        if (ObjectUtil.isAllNotEmpty(bizUserGoodsPageParam.getSortField(), bizUserGoodsPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(bizUserGoodsPageParam.getSortOrder());
            queryWrapper.orderBy(true, bizUserGoodsPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(bizUserGoodsPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(BizUserGoods::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Override
    public Page<BizUserGoods> pageClient(BizUserGoodsPageParam bizUserGoodsPageParam) {
        QueryWrapper<BizUserGoods> queryWrapper = new QueryWrapper<>();
        // 限制
        queryWrapper.lambda().eq(BizUserGoods::getCreateUser, StpLoginUserUtil.getLoginUser().getId());

        if (ObjectUtil.isNotEmpty(bizUserGoodsPageParam.getGoodsName())) {
            queryWrapper.lambda().like(BizUserGoods::getGoodsName, bizUserGoodsPageParam.getGoodsName());
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Autowired
    BizGoodsBoxService bizGoodsBoxService;

    @Override
    public Page<BizUserGoods> pageBus(BizUserGoodsPageParam dto) {
        LambdaQueryWrapper<BizUserGoods> queryWrapper = new LambdaQueryWrapper<>();
        String id = StpLoginUserUtil.getLoginUser().getId();
        // 获取自己的盒子id
        List<BizGoodsBox> bizGoodsBoxes = bizGoodsBoxService.allGoodsBoxByBrandId(id);
        if (ObjectUtil.isEmpty(bizGoodsBoxes)) {
            return new Page<>();
        }
        List<String> boxIdList = bizGoodsBoxes.stream().map(BizGoodsBox::getId).collect(Collectors.toList());
        // 限制只能查看自己商品的订单
        queryWrapper
                .eq(BizUserGoods::getStatus, DbEnum.ORDER_STATUS.PAY.getValue())
                .like(ObjectUtil.isNotEmpty(dto.getUserName()),
                        BizUserGoods::getUserName, dto.getUserName())
                .like(ObjectUtil.isNotEmpty(dto.getGoodsName()),
                        BizUserGoods::getGoodsName, dto.getGoodsName())
                .ge(ObjectUtil.isNotEmpty(dto.getPayMoney()),
                        BizUserGoods::getPayMoney, dto.getPayMoney())
                .in(BizUserGoods::getGoodsBoxId, boxIdList);
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);

    }

    @Override
    public boolean isPaid(String orderId) {
        LambdaQueryWrapper<BizUserGoods> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(BizUserGoods::getGoodsId,orderId)
                .eq(BizUserGoods::getStatus,DbEnum.ORDER_STATUS.NO_PAY.getValue());
        return this.getBaseMapper().exists(queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(BizUserGoodsAddParam bizUserGoodsAddParam) {
        BizUserGoods bizUserGoods = BeanUtil.toBean(bizUserGoodsAddParam, BizUserGoods.class);
        this.save(bizUserGoods);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(BizUserGoodsEditParam bizUserGoodsEditParam) {
        BizUserGoods bizUserGoods = this.queryEntity(bizUserGoodsEditParam.getId());
        BeanUtil.copyProperties(bizUserGoodsEditParam, bizUserGoods);
        this.updateById(bizUserGoods);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<BizUserGoodsIdParam> bizUserGoodsIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(bizUserGoodsIdParamList, BizUserGoodsIdParam::getId));
    }

    @Override
    public BizUserGoods detail(BizUserGoodsIdParam bizUserGoodsIdParam) {
        return this.queryEntity(bizUserGoodsIdParam.getId());
    }

    @Override
    public BizUserGoods queryEntity(String id) {
        BizUserGoods bizUserGoods = this.getById(id);
        if (ObjectUtil.isEmpty(bizUserGoods)) {
            throw new CommonException("订单不存在，id值为：{}", id);
        }
        return bizUserGoods;
    }


}
